package com.study.common.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * Swagger接口文档API
 * @author JJLI
 * @version 1.0
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
	
	/*引入Knife4j提供的扩展类*/
	private final OpenApiExtensionResolver openApiExtensionResolver;
	
	/** 标题 */
	public static final String TITLE = "SmartMonkey接口API";
	/** 描述 */
	public static final String DESCRIPTION = "展示系统所有开放接口API，方便使用者测试和集成本系统接口";
	/** 作者 */
	public static final String AUTHOR = "JJLI";
	/** 站点URL */
	public static final String WEBSITE = "http://smartmonkey.com";
	/** 邮箱 */
	public static final String EMAIL = "jjli@xxxxx.com";
	/** 版本 */
	public static final String VERSION = "V1.0";
	/** 默认分组名称 */
	public static final String GROUP_NAME = "默认";
	
	@Autowired
    public SwaggerConfig(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }
	
	@Bean(value = "defaultApi")
    public Docket defaultApi() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName(GROUP_NAME)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                //赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions(GROUP_NAME));
        return docket;
    }
	
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
			.title(TITLE)
			.description(DESCRIPTION)
			.contact(new Contact(AUTHOR, WEBSITE, EMAIL))
			.version(VERSION)
			.build();
	}

}
